package lxq.asia;

/**
 * 单链表
 */
public class OneWayLink {

    private final LinkNode head;

    public OneWayLink(LinkNode firstNode){
        head=new LinkNode();
        head.next=firstNode;
    }

    public boolean notEmpty(){
        return head.next != null;
    }

    public LinkNode getHead() {
        return head;
    }

    public void insert(LinkNode node){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return;
        }
        var temp=head;

        while (true){
            if (temp.next==null){
                temp.next=node;
                break;
            }
            if (node.order<temp.next.order) {
                node.next=temp.next;
                temp.next=node;
                break;
            }
            if (node.order==temp.next.order){
                System.err.printf("已存在序号为 %d 的节点",node.order);
                return;
            }
            temp=temp.next;
        }
    }

    public void update(LinkNode node){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return;
        }
        var temp=head.next;
        while (temp!=null){
            if (temp.order==node.order) {
                temp.msg=node.msg;
                System.err.printf("已修改节点序号为 %d 的数据",node.order);
                return;
            }
            temp=temp.next;
        }
        System.err.printf("不存在序号为 %d 的节点",node.order);
    }

    public void select(int order){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return;
        }
        var temp=head.next;
        while (temp != null) {
            if (temp.order == order) {
                System.out.println(temp);
                return;
            }
            temp = temp.next;
        }
        System.err.printf("不存在序号为 %d 的节点\n",order);
    }

    public void delete(int order){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return;
        }
        var temp=head.next;
        while (temp != null) {
            if (temp.next.order == order) {
                if (temp.next.next!=null) temp.next=temp.next.next;
                else temp.next=null;
                System.err.printf("已删除节点序号为 %d 的数据\n",order);
                return;
            }
            temp = temp.next;
        }
        System.err.printf("不存在序号为 %d 的节点\n",order);
    }

    public int size(){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return 0;
        }
        var size=1;
        var temp=head.next;
        while (temp.next!=null){
            size++;
            temp=temp.next;
        }
        System.out.println("这个链表有 "+size+" 个节点");
        return size;
    }

    public static void reverse(LinkNode linkNode){
        if (linkNode==null||linkNode.next==null) {
            System.out.println("链表为空或者链表只有一个节点");
            return;
        }
        LinkNode reverseNode=new LinkNode();
        var temp=linkNode;
        while (true){
            if (temp!=null){
                reverseNode.next=temp;
                temp=temp.next;
                continue;
            }
            break;
        }
        linkNode=reverseNode.next;
    }

    public static LinkNode reverse2(LinkNode linkNode){
        if (linkNode==null||linkNode.next==null||linkNode.next.next==null) {
            System.out.println("链表为空或者链表只有一个节点");
            return null;
        }
        var reverseNode=new LinkNode();
        LinkNode next;
        var cur=linkNode.next;
        while (cur != null) {
            next=cur.next;
            cur.next=reverseNode.next;
            reverseNode.next=cur;
            cur=next;
        }
        return reverseNode;
    }

    public void selectFromRear(int index){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return;
        }
        var size=size();
        if (index<=0||size<index){
            System.out.println("无效");
            return;
        }
        var n=size-index;
        var temp=head.next;
        for(var i=0;i<=n;i++){
            if (i==n){
//                System.out.printf("序号：%d ， 数据 ： %s",temp.order,temp.msg);
                System.out.println(temp.toString());
            }
            temp=temp.next;
        }
    }

    public static void printListFromRear(LinkNode node){
        if (node==null) return;
        printListFromRear(node.next);
        System.out.println(node);
    }

    public static LinkNode emerge(LinkNode aNode,LinkNode bNode){
        LinkNode headNode=new LinkNode();
        headNode.msg=aNode.msg;
        headNode.order=aNode.order;
        var headRearNode=headNode.next;
        var next=aNode.next;
        while(next!=null){
            LinkNode node=new LinkNode();
            node.msg=next.msg;
            node.order=next.order;
            headRearNode=node;
            next=next.next;
        }
        return headNode;
    }

    public void printOrderList(){
        if (!notEmpty()) {
            System.out.println("链表为空");
            return;
        }
        var temp=head.next;
        System.out.print("\n[ ");
        while (true){
            if (temp.next!=null){
                System.out.print(temp.order+" , ");
                temp=temp.next;
            }else{
                System.out.print(temp.order);
                break;
            }
        }
        System.out.print(" ]\n");
    }
}
